package org.example.java.exception;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalException {

    @ExceptionHandler(value = StandardException.class)
    @ResponseStatus()
    @ResponseBody
    public ExceptionEntity exceptionHandler(StandardException e) {
        e.printStackTrace();
        return new ExceptionEntity(e.getErrorCode(), e.getMessage());
    }

    @ExceptionHandler(value = NotFoundException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    @ResponseBody
    public ExceptionEntity exceptionHandler(NotFoundException e) {
        return new ExceptionEntity(e.getErrorCode(), e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus()
    @ResponseBody
    public ExceptionEntity exceptionHandler(Exception e) {
        e.printStackTrace();
        return new ExceptionEntity("-1", e.getMessage());
    }
}
